package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLogConstant;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.*;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.PermissionCommand;
import com.cskaoyan.wordmemorize.request.RoleCommand;
import com.cskaoyan.wordmemorize.request.RolePermissionCommand;
import com.cskaoyan.wordmemorize.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin")
public class AdminRoleController {

    @Autowired
    @Qualifier("rolePermissionServiceImpl")
    RolePermissionService rolePermissionService;

    // todo:添加角色
    @SaAdminCheckPermission("admin:role:add")
    @PostMapping("role")
    public Result addRole(@RequestBody RoleCommand roleCommand) {
        rolePermissionService.addRole(roleCommand);
        return Result.ok();
    }

    // todo:修改账号角色
    @SaAdminCheckPermission("admin:role:update")
    @PutMapping("role")
    public Result updateRole(@RequestBody RoleCommand roleCommand) {
        rolePermissionService.updateRole(roleCommand);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:role:authorize")
    @PutMapping("role/permission")
    public Result updateRolePermission(@RequestBody RolePermissionCommand rolePermissionCommand) {
        rolePermissionService.updateRolePermission(rolePermissionCommand);
        return Result.ok();
    }

    // todo:获取角色对应的授权信息(权限树)
    @GetMapping("{roleId}/permission/list")
    public Result<RolePermissionDTO> getRolePermissionList(@PathVariable("roleId") Long roleId) {
        RolePermissionDTO rolePermissionDTO = rolePermissionService.getRolePermissionList(roleId);
        return Result.ok(rolePermissionDTO);
    }

    // todo:账号权限列表(获取权限树)
    @SaAdminCheckPermission("admin:permission:search")
    @GetMapping("permission/list")
    public Result<List<PermissionDTO>> getPermissions() {
        List<PermissionDTO> permissions = rolePermissionService.getPermissions();
        return Result.ok(permissions);
    }

    // todo:获取账号角色列表
    @SaAdminCheckPermission("admin:role:search")
    @GetMapping("role/list")
    public Result<PageDTO<RoleDTO>> getRoleList(PageRequest pageRequest) {
        PageDTO<RoleDTO> roleList = rolePermissionService.getRoleList(pageRequest);
        return Result.ok(roleList);
    }

    // todo:删除账号角色
    @SaAdminCheckPermission("admin:role:delete")
    @DeleteMapping("role/{roleId}")
    public Result deleteRole(@PathVariable("roleId") Long roleId) {
        rolePermissionService.deleteRole(roleId);
        return Result.ok();
    }

    @DeleteMapping("permission/{roleId}/{permissionId}")
    public Result deleteRolePermission(@PathVariable("roleId") Long roleId, @PathVariable("permissionId") Long permissionId) {
        rolePermissionService.deleteRolePermission(roleId, permissionId);
        return Result.ok();
    }


    // todo---------------邵 补充三个API

    // todo 添加账号权限--保存权限树(后台账号权限管理-权限管理)

    @PostMapping("permission")
    public Result savePermissions(@RequestBody PermissionCommand command) {

        // 调用方法
        rolePermissionService.savePermissions(command);

        return Result.ok();
    }

    // todo 修改账号权限--更新权限树(后台账号权限管理-权限管理)
    @PutMapping("permission")
    public Result updatePermission(@RequestBody PermissionCommand command) {

        // 调用方法
        rolePermissionService.updatePermission(command);

        return Result.ok();
    }

    // todo 删除指定账号权限--删除指定权限(后台账号权限管理-权限管理)
    @DeleteMapping("permission/{permissionId}")
    public Result deletePermission(@PathVariable(name = "permissionId") Long permissionId) {
        rolePermissionService.deletePermission(permissionId);
        return Result.ok();
    }


}
